
import { _decorator, Component, Node, Vec2, CircleCollider2D, RigidBody2D, Vec3, Contact2DType, Collider2D, IPhysics2DContact, PhysicsSystem2D, RaycastResult2D } from 'cc';
import { Enemy } from './animy/Enemy';
import { SteelGun } from './animy/SteelGun';
import { ColliderTag, eventTarget, ObjectAtks } from './Globle';
import { MoveObject } from './MoveObject';
import { Player } from './Player';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = RocketChild
 * DateTime = Mon Jan 10 2022 13:23:10 GMT+0800 (中国标准时间)
 * Author = lyps
 * FileBasename = RocketChild.ts
 * FileBasenameNoExtension = RocketChild
 * URL = db://assets/Scripts/RocketChild.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */
 
@ccclass('RocketChild')
export class RocketChild extends MoveObject {
    public ATK = ObjectAtks.thunder;
    private collider: CircleCollider2D;
    private speed: number = 2;
    private isBomb:boolean = false;
    private start_position:Vec3;
    private direction:Vec2;
    //飞行距离
    private fly_distance:number = 100;

    onLoad () {
        this.collider = this.getComponent(CircleCollider2D);
        if (this.collider) this.collider.on(Contact2DType.BEGIN_CONTACT, this.onContact, this);
    }

    private onContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        switch (otherCollider.tag) {
            case ColliderTag.attackedWall:
                eventTarget.emit('destoryAttackedWall',otherCollider.node.uuid);
                break;
            case ColliderTag.enemy://敌人
                let obj = otherCollider.node.getComponent(Enemy);
                if (obj) obj.onContact(selfCollider);
                break;
        }
    }

    public setDirection(direction: Vec2, speed: number, tag: ColliderTag,fly_distance?:number) {
        this.direction = direction;
        this.speed = speed;
        this.start_position = this.node.position.clone();
        if(fly_distance) this.fly_distance = fly_distance;
        this.collider.tag = tag;
    }

    update (deltaTime: number) {
        if(this.isBomb) return;
        super.update(deltaTime);
        this.node.position = new Vec3(this.node.position.x+this.direction.x*this.speed,this.node.position.y+this.direction.y*this.speed);
        let distance = this.node.position.clone().subtract(this.start_position).length();
        if(distance>this.fly_distance) this.node.destroy();
    }

    //射线检测
    onRayCast(rays:Readonly<RaycastResult2D[]>[]) {
        for(let i of rays){
            if(!i||!i.length) continue;
            for(let ray of i){
                switch(ray.collider.tag){
                    case ColliderTag.enemy_people:
                    case ColliderTag.enemy:
                        ray.collider.node.getComponent(Enemy).onContact(this.collider);
                        break;
                    case ColliderTag.attackedWall:
                        eventTarget.emit('destoryAttackedWall',ray.collider.node.uuid);
                        break;
                }
            }
            
        }
    }
}
